package com.zhoug.push.imp;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.ThreadUtils;
import com.zhoug.logging.Logger;
import com.zhoug.push.core.IPushService;
import com.zhoug.push.core.PushCallback;
import com.zhoug.push.core.PushConfig;
import com.zhoug.push.core.PushMessage;
import com.zhoug.push.core.PushVendor;

import java.util.List;

/**
 * 统一推送管理器
 * @description: 类的简要描述
 * @author: 35574
 * @date: 2025/11/4 11:05
 */
public class PushManager {
    private static final String TAG = ">>>PushManager";
    private static PushManager instance;
    private IPushService pushService;
    private Context context;
    private PushCallback callback;

    private PushManager() {
    }

    /**
     * 获取单例
     * @return
     */
    public static PushManager getInstance() {
        if (instance == null) {
            synchronized (PushManager.class) {
                if (instance == null) {
                    instance = new PushManager();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化
     * @param context
     * @param callback
     */
    public void init( @NonNull Context context,  @NonNull PushCallback callback) {
        this.context = context;
        this.callback = callback;
        autoDetectAndCreatePushClient();
    }

    /**
     * 自动检测设备厂商并创建对应的推送客户端
     */
    private void autoDetectAndCreatePushClient() {
        String vendor = getVendor();
        if (vendor == null || PushVendor.HUA_WEI.equals(vendor)) {
            pushService = new HuaweiPushService();
        } else if (PushVendor.XIAO_MI.equals(vendor)) {
            //小米
        }
        if (pushService == null) {
            //默认使用华为推送
            pushService = new HuaweiPushService();
        }
        Logger.d(TAG, "推送服务初始化完成,当前厂商: " + pushService.getVendor());
    }


    /**
     * 注册推送服务
     *
     */
    public void registerPush(PushConfig config) {
        if (pushService != null) {
            pushService.initialize(context, config);
            pushService.registerPush();
        } else {
            LogUtils.e(TAG, "registerPush: 未找到推送服务");
        }
    }


    /**
     * 注销推送服务
     */
    public void unregisterPush() {
        if (pushService != null) {
            pushService.unregisterPush();
        } else {
            LogUtils.e(TAG, "registerPush: 未找到推送服务");
        }
    }


    /**
     * 设置别名（用于精准推送）
     * @param alias
     */
    public void setAlias(String alias) {
        if (pushService != null) {
            pushService.setAlias(alias);
        } else {
            LogUtils.e(TAG, "registerPush: 未找到推送服务");
        }
    }


    /**
     * 设置标签（用于分组推送）
     * @param tags
     */
    public void setTags(List<String> tags) {
        if (pushService != null) {
            pushService.setTags(tags);
        } else {
            LogUtils.e(TAG, "registerPush: 未找到推送服务");
        }
    }

    /**
     * 获取厂商类型
     * @return
     */
    public String getCurrentPushVendor() {
        if (pushService != null) {
            return pushService.getVendor();
        }
        return null;
    }


    /**
     * 获取手机厂商信息
     * @return
     */
    private @Nullable String getVendor() {
        String manufacturer = android.os.Build.MANUFACTURER.toLowerCase();
        String brand = android.os.Build.BRAND.toLowerCase();
        Logger.d(TAG, "设备制造商: " + manufacturer + ", 品牌: " + brand);
        if (manufacturer.contains("huawei") || brand.contains("huawei")
                || manufacturer.contains("honor") || brand.contains("honor")) {
            return PushVendor.HUA_WEI;
        } else if (manufacturer.contains("xiaomi") || brand.contains("xiaomi")) {
            return PushVendor.XIAO_MI;
        } else if (manufacturer.contains("oppo") || brand.contains("oppo")
                || manufacturer.contains("realme") || brand.contains("realme")) {
            return PushVendor.OPPO;
        } else if (manufacturer.contains("vivo") || brand.contains("vivo")) {
            return PushVendor.VIVO;
        }
        return null;
    }

    /**
     * 当收到推送Token时回调
     * @param token 推送Token
     * @param vendor 厂商名称
     */
    void onTokenReceived(String token, String vendor) {
        if (callback != null) {
            ThreadUtils.runMainThread(() -> {
                callback.onTokenReceived(token, vendor);
            });
        }
    }

    /**
     * 当Token被删除时回调
     * @param vendor 厂商名称
     */
    void onTokenRemoved(String vendor) {
        if (callback != null) {
            ThreadUtils.runMainThread(() -> {
                callback.onTokenRemoved(vendor);
            });
        }
    }

    /**
     * 当Token获取失败时回调
     * @param vendor 厂商名称
     * @param errorCode 错误代码
     * @param errorMessage 错误信息
     */
    void onTokenError(String vendor, int errorCode, String errorMessage) {
        if (callback != null) {
            ThreadUtils.runMainThread(() -> {
                callback.onTokenError(vendor, errorCode, errorMessage);
            });
        }
    }

    /**
     * 接受到消息
     * @param vendor 厂商名称
     * @param message 消息
     */
    void onMessageReceived(String vendor, PushMessage message) {
        if (callback != null) {
            ThreadUtils.runMainThread(() -> {
                callback.onMessageReceived(vendor, message);
            });
        }
    }


}
